/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.qingyan.flink.core.trans;

import cn.hutool.core.util.StrUtil;
import com.qingyan.flink.core.properties.FlinkDataSourceProperties;
import com.qingyan.flink.core.properties.FlinkProperties;
import com.qingyan.flink.core.properties.FlinkTransProperties;
import com.qingyan.flink.core.sink.SinkBuilder;
import com.qingyan.flink.core.source.cdc.AbstractCDCBuilder;
import com.qingyan.flink.core.source.cdc.CDCBuilder;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * AbstractOperation
 *
 * @author wenmo
 * @since 2021/6/14 18:18
 */
public abstract class AbstractOperation implements Operation{

    protected static final Logger logger = LoggerFactory.getLogger(AbstractOperation.class);

    protected StreamExecutionEnvironment env;
    protected StreamTableEnvironment tableEnv;
    protected FlinkProperties flinkProperties;
    protected Map<String, FlinkDataSourceProperties> datasource;
    protected FlinkDataSourceProperties sourceProperties;
    protected FlinkDataSourceProperties sinkProperties;
    protected CDCBuilder cdcBuilder;
    protected SinkBuilder sinkBuilder;

    public AbstractOperation() {
    }

    public AbstractOperation(FlinkProperties properties, FlinkTransProperties transProperties) {
        String source = transProperties.getSources();
        String sink = transProperties.getSinks();
        this.flinkProperties = properties;
        this.env = StreamExecutionEnvironment.getExecutionEnvironment();
        this.tableEnv = StreamTableEnvironment.create(env);
        this.datasource = properties.getDatasource();
        this.sourceProperties = datasource.get(source);
        this.sinkProperties = datasource.get(sink);
        this.cdcBuilder = cdc();
        this.sinkBuilder = sink();
    }

    public CDCBuilder cdcBuilder(FlinkDataSourceProperties properties) {
        ServiceLoader<CDCBuilder> builders = ServiceLoader.load(CDCBuilder.class);
        String type = properties.getType();
        for (CDCBuilder builder : builders) {
            if (StrUtil.equals(builder.getHandle(), type)) {
                return builder;
            }
        }
        return null;
    }

    public SinkBuilder sinkBuilder(FlinkDataSourceProperties properties) {
        ServiceLoader<SinkBuilder> builders = ServiceLoader.load(SinkBuilder.class);
        String type = sinkProperties.getType();
        for (SinkBuilder builder : builders) {
            if (StrUtil.equals(builder.getHandle(), type)) {
                builder.create()
            }
        }
        return null;
    }

    @Override
    public String valid() {
        return null;
    }

    public void execute() {
        try {
            DataStreamSource<String> source = cdcBuilder.build(env);
            sinkBuilder.build(cdcBuilder, env, tableEnv, source);
            env.execute(flinkProperties.getPipelineName());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }



}
